# coding:utf-8
# @Time    : 2021/11/10 15:20
# @Author  : Martin
import time

import yaml
from selenium import webdriver
from selenium.webdriver import Remote, DesiredCapabilities
from selenium.webdriver.chrome.options import Options as CH_Options
from selenium.webdriver.firefox.options import Options as FF_Options


class BaseDriver:
    def __init__(self, driver_type):

        self.driver = self._creat_driver(driver_type)
        # 隐式等待
        self.driver.implicitly_wait(5)


    def _creat_driver(self, driver_type):
        driver = None
        if driver_type == "chrome":
            # 本地chrome浏览器
            chrome_options = CH_Options()
            # chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
            # chrome_options.add_experimental_option('excludeSwitches',
            #                                        ['enable-automation', 'enable-logging', 'load-extension'])
            # # 去掉账号密码弹窗
            prefs = {}
            prefs["credentials_enable_service"] = False
            prefs['profile.password_manager_enable'] = False
            chrome_options.add_experimental_option("prefs", prefs)
            driver = webdriver.Chrome(options=chrome_options)
            driver.maximize_window()

        elif driver_type == "chrome-debug":
            # chrome-debug模式
            chrome_options = CH_Options()
            chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
            driver = webdriver.Chrome(options=chrome_options)

        elif driver_type == "firefox":
            # 本地firefox浏览器
            driver = webdriver.Firefox()
            driver.maximize_window()

        elif driver_type == "chrome-headless":
            # chrome headless模式
            chrome_options = CH_Options()
            chrome_options.add_argument("--headless")
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('no-sandbox')
            chrome_options.add_argument('--start-maximized')
            chrome_options.add_argument("--window-size=1920x1080")
            chrome_options.add_argument('lang=zh_CN.UTF-8')
            driver = webdriver.Chrome(options=chrome_options)

        elif driver_type == "firefox-headless":
            # firefox headless模式
            firefox_options = FF_Options()
            firefox_options.headless = True
            driver = webdriver.Firefox(firefox_options=firefox_options)

        elif driver_type == "chrome-grid":
            # DesiredCapabilities
            chrome_options = CH_Options()
            chrome_options.add_argument("--headless")
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('no-sandbox')
            chrome_options.add_argument('--start-maximized')
            chrome_options.add_argument("--window-size=1920x1080")
            chrome_options.add_argument('lang=zh_CN.UTF-8')
            # 通过远程节点运行
            driver = Remote(command_executor='http://192.168.56.10:30001/wd/hub',
                            desired_capabilities={
                                "browserName": "chrome",
                                "javascriptEnabled":True
                            },options=chrome_options)
            # driver.set_window_size(1920, 1080)


        else:
            try:
                driver = getattr(webdriver, driver_type)()
            except Exception as e:
                driver = webdriver.Chrome()
            # raise NameError("{}driver驱动类型定义错误！")


        return driver
